package com.softwinner.awbt.service;

import android.os.RemoteException;
import android.telecom.Call;

import com.goodocom.gocsdk.IGocsdkCallback;
import com.softwinner.awbt.activity.CallActivity;
import com.softwinner.awbt.activity.InComingActivity;
import com.softwinner.awbt.activity.MainActivity;
import com.softwinner.awbt.common.BTApplication;
import com.softwinner.awbt.common.MessageKey;
import com.softwinner.awbt.controller.BTController;
import com.softwinner.awbt.dao.Contact;
import com.softwinner.awbt.fragment.CallLogFragment;
import com.softwinner.awbt.fragment.ContactsFragment;
import com.softwinner.awbt.fragment.MusicFragment;
import com.softwinner.awbt.fragment.SettingsFragment;
import com.softwinner.awbt.listener.CallLogListener;
import com.softwinner.awbt.listener.ContactListener;
import com.softwinner.awbt.listener.MusicListener;
import com.softwinner.awbt.utils.LOG;

/**
 * Created by xiasj on 17-12-19.
 */

public class GocsdkCallbackImp extends IGocsdkCallback.Stub {

    @Override
    public void onHfpDisconnected() throws RemoteException {
        BTApplication.getInstance().setStatus(BTApplication.STATUS_DISCONNECTED);
        BTApplication.getInstance().setCurrentAddress("");
        if (MainActivity.getCallListener() != null) {
            MainActivity.getCallListener().onDisConnected();
        }

        if (SettingsFragment.getListener() != null) {
            SettingsFragment.getListener().onDisconnected();
        }

        if (CallLogFragment.getsCallLogListener() != null) {
            CallLogFragment.getsCallLogListener().onDisconnected();
        }

        if (MusicFragment.getMusicListener() != null) {
            MusicFragment.getMusicListener().onDisconnected();
        }

        ContactListener listener = ContactsFragment.getContactListener();
        if (listener != null) {
            listener.onDisconnected();
        }

        if (CallActivity.getHandler() != null) {
            CallActivity.getHandler().sendEmptyMessage(MessageKey.MSG_DISCONNECT);
        }

        if (InComingActivity.getHandler() != null) {
            InComingActivity.getHandler().sendEmptyMessage(MessageKey.MSG_DISCONNECT);
        }
    }

    @Override
    public void onHfpConnected() throws RemoteException {
        BTApplication.getInstance().setStatus(BTApplication.STATUS_CONNECTED);
        if (MainActivity.getCallListener() != null) {
            MainActivity.getCallListener().onConnected();
        }

        if (SettingsFragment.getListener() != null) {
            SettingsFragment.getListener().onConnected();
        }

        if (CallLogFragment.getsCallLogListener() != null) {
            CallLogFragment.getsCallLogListener().onConnected();
        }

        if (MusicFragment.getMusicListener() != null) {
            MusicFragment.getMusicListener().onConnected();
        }

        ContactListener listener = ContactsFragment.getContactListener();
        if (listener != null) {
            listener.onConnected();
        }
    }

    @Override
    public void onCallSucceed(String number) throws RemoteException {
        BTApplication.getInstance().setStatus(BTApplication.STATUS_CALL_OUT);
    }

    @Override
    public void onIncoming(String number) throws RemoteException {
        BTApplication.getInstance().setStatus(BTApplication.STATUS_INCOMING);
        if (MainActivity.getCallListener() != null) {
            MainActivity.getCallListener().onCallIn(number);
        } else if (BTController.getCallListener() != null) {
            BTController.getCallListener().onCallIn(number);
        }
    }

    @Override
    public void onHangUp() throws RemoteException {
        int status = BTApplication.getInstance().getStatus();
        LOG.d("CocsdkCallbackImp   ---onHangUp status = " + status);
        switch (BTApplication.getInstance().getStatus()) {
            case BTApplication.STATUS_TALKING:
                if (CallActivity.getHandler() != null) {
                    CallActivity.getHandler().sendEmptyMessage(MessageKey.MSG_HANGUP_PHONE);
                }
                break;
            case BTApplication.STATUS_INCOMING:
                if (InComingActivity.getHandler() != null) {
                    InComingActivity.getHandler().sendEmptyMessage(MessageKey.MSG_HANGUP_PHONE);
                }
                break;
            default:
                if (CallActivity.getHandler() != null) {
                    CallActivity.getHandler().sendEmptyMessage(MessageKey.MSG_HANGUP_PHONE);
                }
                break;
        }
        BTApplication.getInstance().setStatus(BTApplication.STATUS_CONNECTED);
    }

    @Override
    public void onTalking(String number) throws RemoteException {
        LOG.d("GocsdkCallbackImp -->onTalking  number--" + number);
        if (BTApplication.getInstance().getStatus() == BTApplication.STATUS_INCOMING) {
            if (InComingActivity.getHandler() != null) {
                LOG.d("GocsdkCallbackImp -->onTalking  to InComingActivity");
                InComingActivity.getHandler().sendEmptyMessage(MessageKey.MSG_INCOMING_CONNECT);
            }
        } else if (BTApplication.getInstance().getStatus() == BTApplication.STATUS_CALL_OUT) {
            if (CallActivity.getHandler() != null) {
                LOG.d("GocsdkCallbackImp -->onTalking  to CallActivity");
                CallActivity.getHandler().sendEmptyMessage(MessageKey.MSG_CALLOUT_CONNECT);
            }
        }


        BTApplication.getInstance().setStatus(BTApplication.STATUS_TALKING);

    }

    @Override
    public void onRingStart() throws RemoteException {

    }

    @Override
    public void onRingStop() throws RemoteException {
    }

    @Override
    public void onHfpLocal() throws RemoteException {

    }

    @Override
    public void onHfpRemote() throws RemoteException {

    }

    @Override
    public void onInPairMode() throws RemoteException {

    }

    @Override
    public void onExitPairMode() throws RemoteException {

    }

    @Override
    public void onInitSucceed() throws RemoteException {

    }

    @Override
    public void onMusicPlaying() throws RemoteException {
        MusicListener listener = MusicFragment.getMusicListener();
        if (listener != null) {
            listener.onPlay();
        }
    }

    @Override
    public void onMusicStopped() throws RemoteException {
        MusicListener listener = MusicFragment.getMusicListener();
        if (listener != null) {
            listener.onPauseMusic();
        }
    }

    @Override
    public void onVoiceConnected() throws RemoteException {

    }

    @Override
    public void onVoiceDisconnected() throws RemoteException {

    }

    @Override
    public void onAutoConnectAccept(boolean autoConnect, boolean autoAccept) throws
            RemoteException {
        if (SettingsFragment.getListener() != null) {
            SettingsFragment.getListener().onAutoAnswer(autoAccept);
            SettingsFragment.getListener().onAutoConnect(autoConnect);
        }
    }

    @Override
    public void onCurrentAddr(String addr) throws RemoteException {
        BTApplication.getInstance().setCurrentAddress(addr);
        if (SettingsFragment.getListener() != null) {
            LOG.d("onCurrentAddress--address=" + addr);
            SettingsFragment.getListener().onCurrentAddress(addr);
        }

        if (CallLogFragment.getsCallLogListener() != null) {
            LOG.d("CallLogListener onCurrentAddress--address=" + addr);
            CallLogFragment.getsCallLogListener().onCurrentAddress(addr);
        }

        if (ContactsFragment.getContactListener() != null) {
            LOG.d("ContactListener  onCurrentAddress--address=" + addr);
            ContactsFragment.getContactListener().onCurrentAddress(addr);
        }

    }

    @Override
    public void onCurrentName(String name) throws RemoteException {
        if (SettingsFragment.getListener() != null) {
            LOG.d("onCurrentDeviceName--name=" + name);
            SettingsFragment.getListener().onCurrentName(name);
        }

    }

    @Override
    public void onHfpStatus(int status) throws RemoteException {
        LOG.i("GocsdkCallbackImp  onHfpStatus status = " + status);
    }

    @Override
    public void onAvStatus(int status) throws RemoteException {

        if (MusicFragment.getMusicListener() != null) {
            LOG.d("GocsdkCallbackImp----onAvStatus  status = " + status);
            MusicFragment.getMusicListener().onStatus(status);
        }
    }

    @Override
    public void onVersionDate(String version) throws RemoteException {

    }

    @Override
    public void onCurrentDeviceName(String name) throws RemoteException {
        LOG.d("onCurrentDeviceName--name=" + name);
        BTApplication.getInstance().setDeviceName(name);
        if (SettingsFragment.getListener() != null) {
            LOG.d("onCurrentDeviceName--name=" + name);
            SettingsFragment.getListener().onCurrentDeviceName(name);
        }
    }

    @Override
    public void onCurrentPinCode(String code) throws RemoteException {

    }

    @Override
    public void onA2dpConnected() throws RemoteException {

    }

    @Override
    public void onCurrentAndPairList(int index, String name, String addr) throws RemoteException {
    }

    @Override
    public void onA2dpDisconnected() throws RemoteException {

    }

    @Override
    public void onPhoneBook(String name, String number) throws RemoteException {
        ContactListener listener = ContactsFragment.getContactListener();
        if (listener != null) {
            listener.onSyncContact(name, number);
        }
    }

    @Override
    public void onSimBook(String name, String number) throws RemoteException {

    }

    @Override
    public void onPhoneBookDone() throws RemoteException {
        ContactListener listener = ContactsFragment.getContactListener();
        if (listener != null) {
            listener.onSyncContactDone();
        }
    }

    @Override
    public void onSimDone() throws RemoteException {

    }

    @Override
    public void onCalllogDone() throws RemoteException {
        CallLogListener listener = CallLogFragment.getsCallLogListener();
        if (listener != null) {
            listener.onCallLogDone();
        }
    }

    @Override
    public void onCalllog(int type, String name, String number) throws RemoteException {
        CallLogListener listener = CallLogFragment.getsCallLogListener();
        if (listener != null) {
            listener.onCallLog(type, name, number);
        }
    }

    @Override
    public void onDiscovery(String name, String addr) throws RemoteException {
        if (SettingsFragment.getListener() != null) {
            SettingsFragment.getListener().onScanDevice(name, addr);
        }
    }

    @Override
    public void onDiscoveryDone() throws RemoteException {
        if (SettingsFragment.getListener() != null) {
            SettingsFragment.getListener().onStopScan();
        }
    }

    @Override
    public void onLocalAddress(String addr) throws RemoteException {

    }

    @Override
    public void onHidConnected() throws RemoteException {

    }

    @Override
    public void onHidDisconnected() throws RemoteException {

    }

    @Override
    public void onMusicInfo(String music_name, String artist_nameString, int duration, int pos,
                            int total) throws RemoteException {
        LOG.d("---》onMusicInfo---name=" + music_name + "   artist=" + artist_nameString);
        MusicListener listener = MusicFragment.getMusicListener();
        if (listener != null) {
            listener.onMusicInfo(music_name, artist_nameString);
        }
    }

    @Override
    public void onOutGoingOrTalkingNumber(String number) throws RemoteException {
        if (MainActivity.getCallListener() != null) {
            MainActivity.getCallListener().onCall(number);
        } else if (BTController.getCallListener() != null) {
            BTController.getCallListener().onCall(number);
        }
    }

    @Override
    public void onConnecting() throws RemoteException {

    }

    @Override
    public void onSppData(int index, String data) throws RemoteException {

    }

    @Override
    public void onSppConnect(int index) throws RemoteException {

    }

    @Override
    public void onSppDisconnect(int index) throws RemoteException {

    }

    @Override
    public void onSppStatus(int status) throws RemoteException {

    }

    @Override
    public void onOppReceivedFile(String path) throws RemoteException {

    }

    @Override
    public void onOppPushSuccess() throws RemoteException {

    }

    @Override
    public void onOppPushFailed() throws RemoteException {

    }

    @Override
    public void onHidStatus(int status) throws RemoteException {

    }

    @Override
    public void onPanConnect() throws RemoteException {

    }

    @Override
    public void onPanDisconnect() throws RemoteException {

    }

    @Override
    public void onPanStatus(int status) throws RemoteException {

    }

    @Override
    public void onProfileEnbled(boolean[] enabled) throws RemoteException {

    }

    @Override
    public void onMessageInfo(String content_order, String read_status, String time, String name,
                              String num, String title) throws RemoteException {

    }

    @Override
    public void onMessageContent(String content) throws RemoteException {

    }


}
